package saveNsend;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
 
import com.cnhis.cloudhealth.clinical.clidoctor.acolsetting.dao.IcolSettingDao;
import com.cnhis.cloudhealth.clinical.clidoctor.clinewadvice.dao.CliNewAdviceDao;
import com.cnhis.cloudhealth.clinical.clidoctor.clisendadvice.bo.CliSendAdviceBo;
import com.cnhis.cloudhealth.clinical.util.AsyncUtil;
import com.cnhis.cloudhealth.clinical.util.ExUtil;
import com.cnhis.cloudhealth.clinical.util.SerializeUtil_prjcli;
import com.cnhis.cloudhealth.clinical.util.SpringUtilV3_prjcli;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.util.oplogutil.AsynUtil;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.onehis.util.DbUtil;
import com.google.common.collect.Maps;

public class insertCviCBM_teset {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		final Logger log = Logger.getLogger(insertCviCBM_teset.class);
		String paramfile = "C:\\0wkspc\\clinical\\src\\test\\java\\saveNsend\\saveNsendIjuParams.txt";
		// final Map map=getmap(paramfile);
		String fullMap = "C:\\0log\\insertCviCbmParamMap2018-04-10 17_12_11_717.map";
		final Map map = (Map) SerializeUtil_prjcli.serizGetObjFromFile(fullMap);
		String json = JSON.toJSONString(map);
		FileUtils.write(new File("c:\\0log\\SaveSendTestParamMap2json.txt"), json);

		SpringUtilV3_prjcli.cfgFileDir = "C:\\0wkspc\\clinical\\springtest_cli";
		SpringUtilV3_prjcli.setLocations("applicationContext-datasource.xml,onehis-dubbo.xml");
		SpringUtilV3_prjcli.iniAppContext();
		System.out.println("dbg");

		for (int i = 0; i < 1; i++) {
			AsynUtil.execMeth_Ays(new Runnable() {

				@Override
				public void run() {

					CliSendAdviceBo bo = (CliSendAdviceBo) SpringUtilV3_prjcli.getBean(CliSendAdviceBo.class);
					geneNewID_v2(map );
					ModelVo mv = new ModelVo();
					try {
						mv = bo.execClinicAdviceSendPretreat(map);
					} catch (Exception e) {
						log.error(e);
						e.printStackTrace();

					}
					System.out.println(JSON.toJSONString(mv, true));

				}
//atitit api method name naming spec 方法命名规范 功能性为主非功能性不要
				private void geneNewID(Map map, CliSendAdviceBo bo) {
					CliNewAdviceDao cliNewAdviceDao = null ;//= bo.getCliNewAdviceDao();
					SqlSession sqlSession = cliNewAdviceDao.getSqlSession();
					log.info(" ---------$$$$$$$$$$$sqlSession_id:" + sqlSession.toString());
					Connection connection = sqlSession.getConnection();
				//	String connstr = JSON.toJSONString(connection);  //if json conn ,haosyo yao check conn close stat 
					log.info("  ----------$$$$$$$$$$$conn_id:" + connection.toString());
					Map logmap = Maps.newConcurrentMap();
					try {
						logmap.put("connection.isClosed()", connection.isClosed());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						logmap.put("connection.isValid(1)", connection.isValid(1));
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				//	logmap.put("connection", connstr);
					log.info("  ----------$$$$$$$$$$$ connection:" + JSON.toJSONString(logmap));
					// connection.isClosed() +connection.isValid(1)
					try {
						geneNewID(map, connection);
					} catch (Exception e) {
						log.error(e);
						ExUtil.throwExV2(e);
						// geneNewID(map,sqlSession);
					}

				}

			 
				synchronized private void geneNewID(Map m, Connection connection) {
					List li = (List) m.get("tmpCBM");
					Map tmpCBM = (Map) li.get(0);
					DbUtil dbu = new DbUtil();
					dbu.conn = connection;
					long startPrkColLong = dbu.getMaxLong("cbm1", "cbm01");
					long cmb01_new = dbu.newNextid(startPrkColLong, 1);
					tmpCBM.put("cbm01", cmb01_new);

				}
				
				
				synchronized private void geneNewID_v2(Map m) {
					List li = (List) m.get("tmpCBM");
					Map tmpCBM = (Map) li.get(0);
					 DbUtil dbu=new DbUtil();
					long startPrkColLong =8340786572203446303l;
					long cmb01_new = dbu.newNextid(startPrkColLong, 1);
					tmpCBM.put("cbm01", cmb01_new);

				}

				synchronized private void geneNewID(Map map, SqlSession sqlSession) {
					// TODO Auto-generated method stub

				}
			}, "threadName");
		}

	}

	@SuppressWarnings("all")
	protected static void geneNewID(Map m, Connection connection) {
		List li = (List) m.get("tmpCBM");
		Map tmpCBM = (Map) li.get(0);
		DbUtil dbu = new DbUtil();
		dbu.conn = connection;
		long startPrkColLong = dbu.getMaxLong("cbm1", "cbm01");
		long cmb01_new = dbu.newNextid(startPrkColLong, 1);
		tmpCBM.put("cbm01", cmb01_new);
	}

	private static Map getmap(String paramfile) {
		String t = "";
		try {
			t = FileUtils.readFileToString(new File(paramfile));
		} catch (IOException e) {
			ExUtil.throwExV2(e);
		}
		String[] a = t.split(":");
		String val = a[1].trim();
		try {
			val = URLDecoder.decode(val, "utf8");
		} catch (UnsupportedEncodingException e) {
			ExUtil.throwExV2(e);
		}

		Map map = JsonUtil.getMap(val);
		// Map m=Maps.newConcurrentMap();
		// m.put(a[0].trim(), val);
		return map;
	}

}
